<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">反馈信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>反馈信息列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="反馈创建时间">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
            <el-form-item label="回复内容">
              <span>{{ props.row.replyContent }}</span>
            </el-form-item>
            <el-form-item label="回复人">
              <span>{{ props.row.replyUserId }}</span>
            </el-form-item>
            <el-form-item label="回复时间">
              <span>{{ props.row.replyTime }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" width="200" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="title" label="内容" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="userId" label="用户名" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="type" label="反馈类型" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="status" label="状态" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 以下是修改反馈信息的弹出表单Dialog -->
    <el-dialog title="修改回复" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="反馈类型" :label-width="formLabelWidth">
          <el-input v-model="editForm.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="回复内容" :label-width="formLabelWidth">
          <el-input v-model="editForm.replyContent" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 100px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  padding-left: 110px;
  margin-right: 10px;
  margin-bottom: 0;
  width: 50%;
}
</style>
<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        type:'',
        replyContent:''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    showEditFormDialog(feedback) {
      this.editForm = feedback;
      this.dialogFormVisible = true;
      console.log(feedback)
      let url = 'http://120.77.154.166:8080/feedback/' + feedback.title+'select';;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://120.77.154.166:8080/feedback/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改品牌成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadCategoryList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadCategoryList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    openDeleteConfirm(feedback) {
      let message = '此操作将永久删除【' + feedback.title + '】反馈信息，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(feedback);
      }).catch(() => {
      });
    },
    handleDelete(feedback) {
      let url = 'http://120.77.154.166:8080/feedback/' + feedback.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' +feedback.title + '】反馈信息成功！',
            type: 'success'
          });
          this.loadFeedbackList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadFeedbackList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    loadFeedbackList() {
      let url = 'http://120.77.154.166:8080/feedback/list';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
          console.log(this.tableData)
        }
      });
    }
  },
  mounted() {
    this.loadFeedbackList();
  }
}
</script>
